package poradnia

class PacjentController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        if(params.nazwisko){
                def pacjenci = Pacjent.findAllByNazwiskoIlike("%" + params.nazwisko + "%", params)
                [pacjentInstanceList: pacjenci, pacjentInstanceTotal: pacjenci.count(), searchNazwisko: params.nazwisko]
        }
        else{
	        [pacjentInstanceList: Pacjent.list(params), pacjentInstanceTotal: Pacjent.count()]
	}
    }

    def create = {
        def pacjentInstance = new Pacjent()
        pacjentInstance.properties = params
        return [pacjentInstance: pacjentInstance]
    }

    def save = {
        def pacjentInstance = new Pacjent(params)
        if (pacjentInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), pacjentInstance.nazwisko+" "+pacjentInstance.imie ])}"
            redirect(action: "show", id: pacjentInstance.id)
        }
        else {
            render(view: "create", model: [pacjentInstance: pacjentInstance])
        }
    }

    def show = {
        def pacjentInstance = Pacjent.get(params.id)
        if (!pacjentInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), params.id])}"
            redirect(action: "list")
        }
        else {
            [pacjentInstance: pacjentInstance]
        }
    }

    def preparePDFView = {
        def pacjentInstance = Pacjent.get(params.id)
        if (!pacjentInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), params.id])}"
            redirect(action: "list")
        }
        else {
	    //TODO prepare visit lists to print - only checked from form
	    // change status of printed visits -> printed
            [pacjentInstance: pacjentInstance]
        }
    }

    def printPDF = {
	def pacjentInstance = Pacjent.get(params.id)
        if (!pacjentInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), params.id])}"
            redirect(action: "list")
        }
        else {
		def wizytyDoWydruku = pacjentInstance?.wizyta
		def wizytyDoWydrukuFiltered = new LinkedList();
		wizytyDoWydruku.each{w->	
			if(params."wizyta_nr_${w.numerWizyty}"){
				if(!w.wydrukowano){
					w.wydrukowano=true;
					w.save()
				}
				wizytyDoWydrukuFiltered.addFirst(w)
			}	
		}
		[pacjentInstance: pacjentInstance, wizytyDoWydruku: wizytyDoWydrukuFiltered]
       }
    }


    def edit = {
        def pacjentInstance = Pacjent.get(params.id)
        if (!pacjentInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [pacjentInstance: pacjentInstance]
        }
    }

    def update = {
        def pacjentInstance = Pacjent.get(params.id)
        if (pacjentInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (pacjentInstance.version > version) {
                    
                    pacjentInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'pacjent.label', default: 'Pacjent')] as Object[], "Another user has updated this Pacjent while you were editing")
                    render(view: "edit", model: [pacjentInstance: pacjentInstance])
                    return
                }
            }
            pacjentInstance.properties = params
            if (!pacjentInstance.hasErrors() && pacjentInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), pacjentInstance.nazwisko+" "+pacjentInstance.imie])}"
                redirect(action: "show", id: pacjentInstance.id)
            }
            else {
                render(view: "edit", model: [pacjentInstance: pacjentInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def pacjentInstance = Pacjent.get(params.id)
        if (pacjentInstance) {
            try {
                pacjentInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pacjent.label', default: 'Pacjent'), params.id])}"
            redirect(action: "list")
        }
    }
}
